package com.example.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.common.Result;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import com.example.demo.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    RoleService roleService;

    /**
     * 分页查询角色
     * @param pageNum
     * @param pageSize
     * @param roleCode
     * @param roleName
     * @return
     */
    @GetMapping("/selectRole")
    public Result<?> selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam(defaultValue = "") String roleCode,
                                @RequestParam(defaultValue = "") String roleName){
        IPage<Role> rolePage = roleService.selectPage(pageNum,pageSize,roleCode,roleName);
        return Result.success(rolePage);
    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @PostMapping("/addRole")
    public Result<?> save(@RequestBody Role role){
        roleService.addRole(role);
        return Result.success();
    }

    /**
     * 更新用户
     * @param role
     * @return
     */
    @PutMapping("/updateRole")
    public Result<?> update(@RequestBody Role role){
        roleService.updateRole(role);
        return Result.success();
    }

    /**
     * 删除用户
     * @param roleId
     * @return
     */
    @GetMapping("/deleteRole")
    public Result<?> delete(@RequestParam Integer roleId){
        roleService.deleteRole(roleId);
        return  Result.success();
    }

    /**
     * 根据角色查询用户
     * @param roleId
     * @return
     */
    @GetMapping("/selectUserByRoleId")
    public Result<?> selectUserByRoleId(@RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        @RequestParam(defaultValue = "") Integer roleId,
                                        @RequestParam(defaultValue = "") String username,
                                        @RequestParam(defaultValue = "") String phoneNumber){
        IPage<User> userListPage = roleService.selectUserByRoleId(pageNum,pageSize,roleId,username,phoneNumber);
        return  Result.success(userListPage);
    }

}
